<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>新增营期</title>
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css?v=2.8.12" />
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
</head>
<body>

<form class="layui-form" action="" lay-filter="create-table-form">

    <div class="mainBox">
        <div class="main-container">
            <div class="layui-tab layui-tab-brief" lay-filter="create-table-tab">
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <table id="column-table" lay-filter="column-table"></table>
                        <script type="text/html" id="column-toolbar">
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                                <i class="layui-icon layui-icon-add-1"></i>新增
                            </button>
                        </script>

                        <script type="text/html" id="col-id">
                            <input type="text" name="columns[{{ d.LAY_NUM-1 }}][id]" placeholder="营期ID" autocomplete="off" class="layui-input" value="{{ d.id }}" disabled>
                        </script>

                        <script type="text/html" id="col-title">
                            <input type="text" name="columns[{{ d.LAY_NUM-1 }}][goods_period_title]" placeholder="营期标题" autocomplete="off" class="layui-input" value="{{ d.goods_period_title }}">
                        </script>

                        <script type="text/html" id="col-field">
                            <input type="text" name="columns[{{ d.LAY_NUM-1 }}][goods_period_date]" placeholder="营期日期" autocomplete="off" class="layui-input" value="{{ d.goods_period_date }}">
                        </script>

                        <script type="text/html" id="col-sale-num">
                            <input type="text" name="columns[{{ d.LAY_NUM-1 }}][goods_sale_num]" placeholder="销售数量" autocomplete="off" class="layui-input" value="{{ d.goods_sale_num }}">
                        </script>

                        <script type="text/html" id="col-stock-num">
                            <input type="text" name="columns[{{ d.LAY_NUM-1 }}][goods_stock_num]" placeholder="商品库存" autocomplete="off" class="layui-input" value="{{ d.goods_stock_num }}">
                        </script>

                        <script type="text/html" id="col-min-group-num">
                            <input type="number" name="columns[{{ d.LAY_NUM-1 }}][min_group_num]" placeholder="最小成团数" autocomplete="off" class="layui-input" value="{{ d.min_group_num }}">
                        </script>

                        <script type="text/html" id="col-nullable">
                            <input type="checkbox" name="columns[{{ d.LAY_NUM-1 }}][status]" autocomplete="off" class="layui-input" lay-skin="primary" value="{{ d.status }}" {{ d.status ? 'checked' : '' }}>
                        </script>

                    </div>

                </div>
            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                    lay-filter="save">
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
                重置
            </button>
        </div>
    </div>
</form>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script>

    const CREATE_API = "/app/admin/rl-goods/updatePeriod";
    const Period_API = "/app/admin/rl-goods/period";
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    // 字段设置
    layui.use(["table", "common", "popup","jquery"], function () {
        window._field_id = 0;
        let table = layui.table;
        let $ = layui.jquery;
        let cols = [
            {
                title: "ID",
                field: "id",
                templet: "#col-id",
                width: '10%',
            },
            {
                title: "营期标题",
                field: "goods_period_title",
                templet: "#col-title",
                width: '20%',
            },
            {
                title: "营期日期",
                field: "goods_period_date",
                templet: "#col-field",
                width: '20%',
            },
            {
                title: "销量",
                field: "goods_sale_num",
                templet: "#col-sale-num",
                width: '15%',
            },
            {
                title: "库存",
                field: "goods_stock_num",
                templet: "#col-stock-num",
                width: '15%',
            },
            {
                title: "最小成团数",
                field: "min_group_num",
                templet: "#col-min-group-num",
                width: '15%',
            },
            {
                title: "状态",
                field: "status",
                templet: "#col-nullable",
                width: '20%',
                align: "center",
            }
        ];

        $(document).ready(function(){
            console.log('页面加载完成');
            layui.use(["jquery"], function() {
                layui.$.ajax({
                    url: Period_API,
                    dataType: "json",
                    type: "POST",
                    data: {'goods_id':getUrlParam('id')},
                    success: function (res) {
                        table.render({
                            elem: "#column-table",
                            cols: [cols],
                            data: res.data,
                            cellMinWidth: 40,
                            skin: "line",
                            size: "lg",
                            limit: 10000,
                            page: false,
                            toolbar: "#column-toolbar",
                            defaultToolbar: [],
                        });
                    }
                });
            });
        });

        table.on("toolbar(column-table)", function(obj) {
            if (obj.event === "add") {
                add();
            }
        });

        let add = function() {
            let tableData = layui.form.val("create-table-form");
            let columnTableData = [];

            let len = (Object.keys(tableData).length)/5;

            let id = 0;
            while (id < len) {
                if (typeof tableData["columns[" + id + "][id]"] !== "undefined") {
                    columnTableData.push({
                        id : tableData["columns[" + id + "][id]"],
                        goods_period_title: tableData["columns[" + id + "][goods_period_title]"],
                        goods_period_date: tableData["columns[" + id + "][goods_period_date]"],
                        goods_sale_num: tableData["columns[" + id + "][goods_sale_num]"],
                        min_group_num: tableData["columns[" + id + "][min_group_num]"],
                        goods_stock_num: tableData["columns[" + id + "][goods_stock_num]"],
                        status: tableData["columns[" + id + "][status]"]
                    });
                }
                id++;
            }
            columnTableData.push({
                id : 0,
                goods_period_title: "",
                goods_period_date: "",
                goods_sale_num: 0,
                goods_stock_num: 0,
                status: 1,
            });
            table.reloadData("column-table", {data:columnTableData});
        }

    });

    layui.use(["form", "popup"], function () {
        //提交事件
        layui.form.on("submit(save)", function () {
            let data = layui.form.val("create-table-form");
            data.goods_id=getUrlParam('id');
            layui.$.ajax({
                url: CREATE_API,
                type: "POST",
                dateType: "json",
                data: data,
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        parent.refreshTable();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }
            });
            return false;
        });
    });

</script>

</body>
</html>
